﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Tools.Web.Models
{
    public class ResultMessage
    {
        /// <summary>
        /// 错误编码
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 错误信息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 附加信息
        /// </summary>
        public object Info { get; set; }

        /// <summary>
        /// 响应状态是否成功
        /// </summary>
        public bool IsSuccess { get { return Code == 0 && Message == "success"; } }

        /// <summary>
        /// 响应成功信息
        /// </summary>
        /// <returns></returns>
        public ResultMessage Success()
        {
            return Success(null);
        }

        /// <summary>
        /// 响应成功信息
        /// </summary>
        /// <param name="info">附加信息</param>
        /// <returns></returns>
        public ResultMessage Success(object info)
        {
            return new ResultMessage() { Code = 0, Message = "success", Info = info };
        }


        /// <summary>
        /// 响应失败信息
        /// </summary>
        /// <param name="message">失败信息</param>
        /// <returns></returns>
        public ResultMessage Failure(string message)
        {
            return Failure(-1, message, null);
        }

        /// <summary>
        /// 响应失败信息
        /// </summary>
        /// <param name="message">失败信息</param>
        /// <param name="info">附加信息</param>
        /// <returns></returns>
        public ResultMessage Failure(string message, object info)
        {
            return Failure(-1, message, info);
        }

        /// <summary>
        /// 响应失败信息
        /// </summary>
        /// <param name="code">错误编码</param>
        /// <param name="message">错误信息</param>
        /// <returns></returns>
        public ResultMessage Failure(int code, string message)
        {
            return Failure(code, message, null);
        }

        /// <summary>
        /// 响应失败信息
        /// </summary>
        /// <param name="code">错误编码</param>
        /// <param name="message">错误信息</param>
        /// <param name="info">附加信息</param>
        /// <returns></returns>
        public ResultMessage Failure(int code, string message, object info)
        {
            return new ResultMessage() { Code = code, Message = message, Info = info };
        }


        /// <summary>
        /// 响应异常信息
        /// </summary>
        /// <param name="ex">异常信息</param>
        /// <returns></returns>
        public ResultMessage Error(Exception ex)
        {
            return Error(-1000, ex, null);
        }

        /// <summary>
        /// 响应失败信息
        /// </summary>
        /// <param name="ex">异常信息</param>
        /// <param name="info">附加信息</param>
        /// <returns></returns>
        public ResultMessage Error(Exception ex, object info)
        {
            return Error(-1000, ex, info);
        }

        /// <summary>
        /// 响应异常信息
        /// </summary>
        /// <param name="code">错误编码</param>
        /// <param name="ex">异常信息</param>
        /// <returns></returns>
        public ResultMessage Error(int code, Exception ex)
        {
            return Error(code, ex, null);
        }

        /// <summary>
        /// 响应异常信息
        /// </summary>
        /// <param name="code">错误编码</param>
        /// <param name="ex">异常信息</param>
        /// <param name="info">附加信息</param>
        /// <returns></returns>
        public ResultMessage Error(int code, Exception ex, object info)
        {
            return new ResultMessage() { Code = code, Message = ex.Message, Info = info };
        }
    }
}
